package org.whp.mealplanner.server;

import java.io.IOException;

import java.util.Enumeration;


import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RemoveIngredientsServlet extends HttpServlet {
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Enumeration paramNames = req.getParameterNames();
		resp.setContentType("text/plain");
		resp.getWriter().println(paramNames.hasMoreElements());
		while(paramNames.hasMoreElements()) {
			
			String name = (String)paramNames.nextElement();
			String value = req.getParameter(name);
			if (!name.equals("removeIngredients")) {
				//Key k = KeyFactory.createKey(Ingredient.class.getSimpleName(), name);
				Ingredient ingredientForRemoval = pm.getObjectById(Ingredient.class, name);
				if (ingredientForRemoval != null){
					resp.getWriter().println("To remove " + ingredientForRemoval.getName());
					try {
						pm.deletePersistent(ingredientForRemoval);
				} finally {
					
				}
			}else {}
			
			resp.getWriter().println("Key: " + name + ", Value: " + value);		
			
		}
		
		
		}		
		pm.close();
		
	}
}
